package com.nali.dal.statement.function.formatter.date;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nali.dal.statement.function.AbstractDataHandler;
import com.nali.dal.statement.function.FunctionArgument;
import com.nali.dal.statement.function.FunctionException;

/**
 * TruncateLong<br>&nbsp;
 * 对long存储类型的时间进行格式化，默认当秒处理
 * @author sam Created on 2011-4-16
 */
@Component
public class TruncateNumberDate extends AbstractDataHandler<Date> {
	@Autowired
	private Truncate truncater;
	
	@Override
	public void validate(List<FunctionArgument> arguments) throws FunctionException {
		checkSize(arguments, 2, 3);
	}

	@Override
	public Date handle(List<Object> argList) throws FunctionException {
		long time = ((Number) argList.get(0)).longValue();
		String toUnitName = (String) argList.get(1);
		TimeUnit fromUnit;
		if(argList.size()>2) {
			fromUnit = TimeUnit.getTimeUnit((String) argList.get(2));
		} else {
			fromUnit = TimeUnit.SECOND;
		}
		return truncater.handle(fromUnit.getDate(time), toUnitName);
	}
	
}

